#include "Thread.h"
#include <unistd.h>
#include <iostream>
#include <memory>

using std::cout;
using std::endl;
using std::unique_ptr;

class MyTask
{
public:
    void process() 
    {
        //需要去实现具体的逻辑
        while(1)
        {
            cout << "MyTask is running!" << endl;
            sleep(1);
        }
    }
};

void func(int x)
{
    while(1)
    {
        cout << "func is running!" << endl;
        sleep(x);
    }
}

void test()
{
    MyTask task;//栈对象
    Thread th(std::bind(&MyTask::process, &task));

    th.start();
    th.join();
}

void test2()
{
    MyTask task;//栈对象
    //bind改变了函数process的形态
    function<void()> fun = std::bind(&MyTask::process, &task);
    Thread th(std::move(fun));

    th.start();
    th.join();
}

void test3()
{
    MyTask task;
    unique_ptr<Thread> pth1(new Thread(std::bind(&MyTask::process, &task))); //创建堆对象
    pth1->start();
    pth1->join();
}

void test4()
{
    unique_ptr<Thread> pth1(new Thread(std::bind(&func, 3))); //创建堆对象
    /* unique_ptr<Thread> pth1(new Thread(std::move(func))); //创建堆对象 */
    pth1->start();
    pth1->join();
}
int main(int argc, char **argv)
{
    test4();
    return 0;
}

